//
//  ConfirmViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/18.
//  Copyright © 2016年 Jack. All rights reserved.
//   确认订单

#import "ConfirmViewController.h"
#import "HeaderFileForEasyToTesco.h"


#import "OrderDetailTableViewCell.h"
#import "OrderPayTableViewCell.h"
#import "ConfirmOrderNumTableViewCell.h"
//beizhu
#import "TextfeildTableViewCell.h"


#import "UIImageView+WebCache.h"
#import "AFNetworking.h"


//订单支付
#import "OrderPayViewController.h"
#import "ConfirmTableViewCell.h"

//获取收货地址
#import "GetAddressViewController.h"

<<<<<<< HEAD
#import "qqTableViewCell.h"

@interface ConfirmViewController ()<UITableViewDataSource, UITableViewDelegate, ConfirmOrderNumTableViewCelldelegate>

@property (nonatomic, strong)UITableView *confirmtableview;
@property (nonatomic, assign) float allRate;
@property (nonatomic, assign) int allYunFei;
=======


@interface ConfirmViewController ()<UITableViewDataSource, UITableViewDelegate, ConfirmOrderNumTableViewCelldelegate>{

    //总价
    NSString *price;
    //数量
    NSInteger goodsNum;
}

@property (nonatomic, strong)UITableView *confirmtableview;
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee

@end

@implementation ConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
<<<<<<< HEAD
    self.allRate = 0;
    self.allYunFei = 0;
=======
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    // Do any additional setup after loading the view from its nib.
    
    [self addconfirminitsth];
    
    [self addconfirmtopviewUI];
    
    [self addconfirmtableviewUI];
    //加载底部视图
    [self addconfirmlastviewlUI];
    
}

- (void)addconfirminitsth {
<<<<<<< HEAD
//    goodsNum = [self.confirmDic[@"goodsCount"] integerValue];
//    price = self.confirmDic[@"huiyuanjia"];
=======
    goodsNum = 1;
    price = self.confirmDic[@"huiyuanjia"];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
}

- (void)addconfirmtopviewUI {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"确认订单";

}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)addconfirmtableviewUI {
    self.confirmtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.83) style:UITableViewStylePlain];
    self.confirmtableview.delegate = self;
    self.confirmtableview.dataSource = self;
    self.confirmtableview.tableFooterView =[[UIView alloc] init];
    self.confirmtableview.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
    [self.view addSubview:self.confirmtableview];
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
<<<<<<< HEAD
    if (section == 1) {
        return self.goodsInfoArrM.count;
    } else if (section == 4) {
        return 0;
    } else if (section == 3) {
        return 2;
    } else {
        return 1;
=======
    if ((section == 0)||(section == 1)||(section == 3)) {
        return 1;
    } else {
        return 5;
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        //显示收货地址
        static NSString *identifier = @"OrderDetailTableViewCell1";
<<<<<<< HEAD
        qqTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qqtabeviecel"];
        if (cell == nil) {
//            cell = [[[NSBundle mainBundle]loadNibNamed:@"OrderDetailTableViewCell" owner:nil options:nil] lastObject];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            
            cell = [[qqTableViewCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qqtabeviecel"];
            
            cell.Name.text = @" 会员 123";
             cell.PhoneNum.text = @" 18043782910";
            cell.Address.text = @"浙江省 杭州市  文二路 西湖国际科技大厦";
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
        
        
=======
        OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"OrderDetailTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        return cell;
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    } else if (indexPath.section == 1) {
        static NSString *identifier = @"ConfirmTableViewCell";
        ConfirmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ConfirmTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
<<<<<<< HEAD
        NSString *str1 = self.goodsInfoArrM[indexPath.row][@"picurl"];
        NSString *str2 = self.goodsInfoArrM[indexPath.row][@"picprex"][@"s"];
        NSString *str3 = [str2 stringByAppendingString:str1];
        [cell.ConfirmPicImage sd_setImageWithURL:[NSURL URLWithString:str3]];
        cell.ConfirmTitleLabel.text = [NSString stringWithFormat:@"%@", self.goodsInfoArrM[indexPath.row][@"title"]];
        cell.confirmMoneylabel.text = [NSString stringWithFormat:@"%@积分", self.goodsInfoArrM[indexPath.row][@"jiage"]];
        
        cell.yunfeiLabel.text = [NSString stringWithFormat:@"运费:%@积分", self.goodsInfoArrM[indexPath.row][@"jzh"]];
        
        int num = [self.goodsInfoArrM[indexPath.row][@"feilv"] intValue];
        if (num != 0) {
            cell.rateLabel.text = [NSString stringWithFormat:@"费率:%@%%", self.goodsInfoArrM[indexPath.row][@"feilv"]];
            NSLog(@"%d-----%d------%d", [self.goodsInfoArrM[indexPath.row][@"jiage"] intValue], [self.goodsInfoArrM[indexPath.row][@"feilv"] intValue], [self.goodsInfoArrM[indexPath.row][@"nums"] intValue]);
            int feilv = [self.goodsInfoArrM[indexPath.row][@"feilv"] intValue];
            int nums = [self.goodsInfoArrM[indexPath.row][@"nums"] intValue];
            int jiage = [self.goodsInfoArrM[indexPath.row][@"jiage"] intValue];
            float oneFeilv = jiage * nums * feilv * 0.01 * 1.0;
            self.allRate = self.allRate + oneFeilv;
            NSLog(@"%f", self.allRate);
#warning -------------------运费未算
            
        } else {
            cell.rateLabel.text = @"";
        }
        cell.ConfirmNumLabel.text = [NSString stringWithFormat:@"×%@", self.goodsInfoArrM[indexPath.row][@"nums"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 2) {
        if (indexPath.row == 4){
=======
        
        [cell.ConfirmPicImage sd_setImageWithURL:[NSURL URLWithString:self.confirmDic[@"picImageStr"]] placeholderImage:[UIImage imageNamed:@"cell.png"]];
        cell.ConfirmTitleLabel.text = [NSString stringWithFormat:@"%@", self.confirmDic[@"title"]];
        cell.confirmMoneylabel.text = [NSString stringWithFormat:@"%@", self.confirmDic[@"huiyuanjia"]];
        cell.ConfirmNumLabel.text = [NSString stringWithFormat:@"×%ld", goodsNum];
        return cell;
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {//数量修改
            ConfirmOrderNumTableViewCell *numCell = [ConfirmOrderNumTableViewCell cellWithTableView:tableView];
            numCell.ConfirmNumberLabel.text = [NSString stringWithFormat:@"%ld", goodsNum];
            numCell.delegate = self;
            return numCell;
        } else if (indexPath.row == 4){
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
            static NSString *identifier = @"TextfeildTableViewCell";
        TextfeildTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"TextfeildTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        }
            return cell;
        }else {
            static NSString *identifier = @"zanding1";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.textLabel.text = @"配送方式";
            cell.detailTextLabel.text= @"快递 免邮监控空接口接口很快很快会很快";
            
            return cell;
        }
    } else {
        static NSString *identifier = @"zanding2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
<<<<<<< HEAD
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == 0) {
            cell.textLabel.text = @"费率";
            cell.detailTextLabel.text = [NSString stringWithFormat:@"-%.2f", self.allRate];
        } else {
            cell.textLabel.text = @"运费";
            cell.detailTextLabel.text = @"+";
        }
=======
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        return cell;
    }
}

//增加按钮代理方法
<<<<<<< HEAD
//- (void)addBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
//    goodsNum += 1;
//    [self.confirmtableview reloadData];
//}
//减少按钮代理方法Reduction
//- (void)reductionBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
//    if (goodsNum<=1) {
//        goodsNum = 1;
//        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经无法再减少了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alview show];
//    } else {
//        goodsNum -= 1;
//    }
//    [self.confirmtableview reloadData];
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
=======
- (void)addBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
    goodsNum += 1;
    [self.confirmtableview reloadData];
}
//减少按钮代理方法Reduction
- (void)reductionBtnClick:(UITableViewCell *)cell andbutton:(UIButton *)sender {
    if (goodsNum<=1) {
        goodsNum = 1;
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经无法再减少了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    } else {
        goodsNum -= 1;
    }
    [self.confirmtableview reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
<<<<<<< HEAD
        return 110;
    } else if (indexPath.section == 1) {
        return 110;
        
    }else {
        return 60;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 4 || section == 3) {
        return 35;
    } else {
        return 2;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5.01;
=======
        return 80;
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 80;
        } else if (indexPath.row == 1){
        return 80;
        } else {
            return 30;
        }
    }else {
        return 30;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 3) {
        return 30;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerview = [[UIView alloc] init];
    headerview.backgroundColor = [UIColor grayColor];
    
<<<<<<< HEAD
    if (section == 4 || section == 3) {
        UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 160, 20)];
        numLabel.text = [NSString stringWithFormat:@"共计%ld件商品", self.goodsInfoArrM.count];
=======
    if (section == 3) {
        UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 160, 20)];
        numLabel.text = [NSString stringWithFormat:@"共计%ld件商品", goodsNum];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        [headerview addSubview:numLabel];
        
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(WEITH - 88, 5, 80, 20)];
        priceLabel.textColor = [UIColor redColor];
        priceLabel.font = [UIFont systemFontOfSize:14];
<<<<<<< HEAD
        priceLabel.text = [NSString stringWithFormat:@"¥%.2f", self.allPrice];
=======
        priceLabel.text = [NSString stringWithFormat:@"¥%.2f", [price floatValue]*goodsNum];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        
        [headerview addSubview:priceLabel];
        
        UILabel *otherLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(priceLabel.frame)-50, 5, 50, 20)];
        otherLabel.text = @"合计:  ";
        
        otherLabel.textAlignment = NSTextAlignmentRight;
        otherLabel.font = [UIFont systemFontOfSize:14];
        [headerview addSubview:otherLabel];

    }
    
    
    return headerview;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    return footview;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView endEditing:YES];
    if (indexPath.section == 0) {
        GetAddressViewController *getAddressVC = [[GetAddressViewController alloc] init];
        [self presentViewController:getAddressVC animated:YES completion:nil];
    }
    
}

//添加UI
- (void)addconfirmlastviewlUI {
    
    //底层的view
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, HIGHT*0.93, (WEITH/3)*2, HIGHT*0.068)];
    firstView.backgroundColor = [UIColor whiteColor];
    firstView.layer.borderWidth = .6;
    firstView.layer.borderColor = [UIColor grayColor].CGColor;//[UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1].CGColor;
    [self.view addSubview:firstView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake((WEITH/3)*2, HIGHT*0.93, WEITH/3, HIGHT*0.068);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sureBtn.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    
    UILabel *translabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(firstView.frame)-60, 5, 60, CGRectGetHeight(firstView.frame)-10)];
<<<<<<< HEAD
    translabel.text = @"含运费";
=======
    translabel.text = @"不含运费";
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    translabel.font = [UIFont systemFontOfSize:11];
    [firstView addSubview:translabel];
    
    UILabel *Allprice = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(translabel.frame)-80, 5, 80, CGRectGetHeight(firstView.frame)-10)];
    Allprice.textColor = [UIColor redColor];
<<<<<<< HEAD
    Allprice.text = [NSString stringWithFormat:@"¥%.2f", self.allPrice];
=======
    Allprice.text = [NSString stringWithFormat:@"¥%.2f", [price floatValue]];
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    Allprice.textAlignment = NSTextAlignmentLeft;
    Allprice.font = [UIFont systemFontOfSize:12.5];
    [firstView addSubview:Allprice];
    
    UILabel *otherlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(Allprice.frame)-60, 5, 60, CGRectGetHeight(firstView.frame)-10)];
    otherlabel.text = @"合计:  ";
    otherlabel.textAlignment = NSTextAlignmentRight;
    otherlabel.font = [UIFont systemFontOfSize:12.5];
    [firstView addSubview:otherlabel];
    
}

- (void)sureBtnClick{
    OrderPayViewController *orderpayVC = [[OrderPayViewController alloc] init];
    [self presentViewController:orderpayVC animated:YES completion:nil];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
